package com.magic.smartcity.view.more.viewmodels

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.magic.smartcity.beans.ActivityCategoryList
import com.magic.smartcity.beans.ActivityComment
import com.magic.smartcity.beans.ActivityDetail
import com.magic.smartcity.beans.ActivityRotation
import com.magic.smartcity.beans.pojo.ActivityCommentBody
import com.magic.smartcity.view.more.models.ActivityModel

/**
 * @description:
 * @create: Create by Magic on 2022/2/15.
 */
class ActivityViewModel : ViewModel() {
    private val TAG: String = "ActivityViewModel"
    val model = ActivityModel()

    val rotaionList: MutableLiveData<List<ActivityRotation>> = MutableLiveData()
    val category: MutableLiveData<List<ActivityCategoryList>> = MutableLiveData()
    val activityList: MutableLiveData<List<ActivityDetail>> = MutableLiveData()
    val activityDetail: MutableLiveData<ActivityDetail> = MutableLiveData()
    val commentList: MutableLiveData<List<ActivityComment>> = MutableLiveData()
    val isCommentOk: MutableLiveData<String> = MutableLiveData()
    val error: MutableLiveData<Int> = MutableLiveData()
    val isSignup: MutableLiveData<Boolean> = MutableLiveData()
    val signOk:MutableLiveData<String> = MutableLiveData()

    fun getActivityRotationImages() {
        Log.d(TAG, "请求获取活动轮播图")
        model.getActivityRotationImage(success = {
            rotaionList.postValue(it)
        }, error = {})
    }

    fun getActivityCategory() {
        Log.d(TAG, "请求获取活动分类")
        model.getActivityCategory(success = {
            category.postValue(it)
        }, error = {})
    }

    fun getActivityList() {
        Log.d(TAG, "请求获取活动列表")
        model.getActivityList(success = {
            activityList.postValue(it)
        }, error = {})
    }

    fun getActivityById(id: Int) {
        Log.d(TAG, "请求获取指定活动id=${id}")
        model.getActivityById(id, success = {
            activityDetail.postValue(it)
        }, error = {})
    }

    fun getActivityCommentById(id: Int) {
        Log.d(TAG, "请求获取指定活动的评论列表id=${id}")
        model.getActivityCommentById(id, success = {
            commentList.postValue(it)
        }, error = {})
    }

    fun activityComment(activityId: Int, content: String) {
        val activityComment = ActivityCommentBody()
        activityComment.activityId = activityId
        activityComment.content = content
        Log.d(TAG, "提交了评论至活动id=$activityId")
        model.activityComment(activityComment, success = {
            isCommentOk.postValue(it)
        }, error = { error.postValue(it.code) })
    }

    fun userIsSignup(activityId: Int) {
        Log.d(TAG, "请求获取用户是否已经报名id=${activityId}")
        model.activityIsSignup(activityId, success = {
            isSignup.postValue(it)
        }, error = {})
    }

    fun signup(activityId: Int) {
        Log.d(TAG,"用户报名活动id=${activityId}")
        model.userSignupByActivityId(activityId, success = {
            signOk.postValue(it)
        }, error = {})
    }
}